package com.bookshop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bookshop.entity.Permission;
import com.bookshop.entity.User;
import com.bookshop.mapper.UserMapper;
import com.bookshop.service.UserService;
import com.bookshop.util.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author g2l
 * @since 2024-01-13
 */
@Service
public class UserServiceImp extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public long login(String phoneNumber, String password) {

        // 查询用户
        User user = getUserByPhoneNumber(phoneNumber);

        // 无用户
        if (user == null) return -1;

        boolean equals = user.getPassword().equals(MD5Util.getPassword(password));
        // 返回用户等级
        if (equals) return user.getRoleId();

        return -1;
    }

    @Override
    public long smsLogin(String phoneNumber) {
        // 查询用户
        User user = getUserByPhoneNumber(phoneNumber);

        // 无用户
        if (user == null) return -1;

        // 正常登录
        return 1;
    }

    @Override
    public int registerBySms(String phoneNumber, String password) {

        // 该手机号已注册
        if (getUserByPhoneNumber(phoneNumber) == null) return -1;

        // 组装用户信息
        User user = new User();
        user.setRoleId(0L);
        user.setPhoneNum(phoneNumber);
        user.setUsername(phoneNumber);
        user.setPassword(MD5Util.getPassword(password));
        user.setGender("保密");
        user.setCreateTime(LocalDateTime.now());

        return userMapper.insert(user);
    }

    @Override
    public int register(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int updatePassword(String phoneNumber, String newPassword) {
        User user = getUserByPhoneNumber(phoneNumber);
        // 无用户
        if (user == null)return -1;

        user.setPassword(MD5Util.getPassword(newPassword));

        return userMapper.updateById(user);
    }

    @Override
    public int updateUsername(String phoneNumber, String newUsername) {
        User user = getUserByPhoneNumber(phoneNumber);
        // 无用户
        if (user == null)return -1;

        user.setUsername(newUsername);

        return userMapper.updateById(user);
    }

    @Override
    public int updatePhoneNumber(String phoneNumber, String newPhoneNumber) {
        User user = getUserByPhoneNumber(phoneNumber);
        // 无用户
        if (user == null)return -1;

        user.setPhoneNum(newPhoneNumber);

        return userMapper.updateById(user);
    }

    @Override
    public int updatePicture(String phoneNumber, String newPicture) {
        User user = getUserByPhoneNumber(phoneNumber);
        // 无用户
        if (user == null)return -1;

        user.setPicture(newPicture);

        return userMapper.updateById(user);
    }

    @Override
    public int updateRoleId(String phoneNumber, Long newRoleId) {
        User user = getUserByPhoneNumber(phoneNumber);
        // 无用户
        if (user == null)return -1;

        user.setRoleId(newRoleId);

        return userMapper.updateById(user);
    }

    @Override
    public List<Permission> getPermissions(String phoneNumber) {
        return userMapper.selectPermissions(phoneNumber);
    }

    // 查询用户
    @Override
    public User getUserByPhoneNumber(String phoneNumber) {
        // 返回null则说明没找到
        return getUser("phone_num", phoneNumber);
    }

    @Override
    public int deleteUser(User user) {
        return userMapper.deleteById(user.getId());
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public User getUserByUsername(String nickname) {
        return getUser("username", nickname);
    }

    @Override
    public Map<String, String> getBasicInfo(int userId) {
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("cartNum" , String.valueOf(userMapper.getCartInfo(userId)));
        resultMap.put("favoritesNum" , String.valueOf(userMapper.getFavoritesInfo(userId)));
        resultMap.put("payNum" , String.valueOf(userMapper.getPayInfo(userId , 1)));
        resultMap.put("unPayNum" , String.valueOf(userMapper.getUnPayInfo(userId , 0)));
        resultMap.put("outTimeNum" , String.valueOf(userMapper.getOutTimeInfo(userId , 2)));
        resultMap.put("picture" , userMapper.getUserPicture(userId));
        return resultMap;
    }

    private User getUser(String columnName, Object value){
        // 查出对应对象
        HashMap map = new HashMap();
        map.put(columnName, value);
        List<User> users = userMapper.selectByMap(map);

        if (users.size() == 0){
            return null;
        }

        return users.get(0);
    }
}
